package com.ancai.fragment;

import java.util.ArrayList;
import java.util.List;
import com.ancai.bean.PicBean;
import com.ancai.center.activity.WebActivity;
import com.ancai.studentmenage.R;
import com.pac.base.adapter.CommonAdapter;
import com.pac.base.adapter.ViewHolder;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class CenterFragment extends Fragment {
	private List<PicBean> picList = new ArrayList<PicBean>();
	private String[] nameArray = { "百度", "阿里巴巴", "腾讯网" };
	private String[] picArray = {
			"https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png",
			"https://ss1.baidu.com/6ONXsjip0QIZ8tyhnq/it/u=1361009878,2464825863&fm=58",
			"https://ss1.bdstatic.com/9qV1bjeh1BF3odCf/zhanzhang/zzlogo/0f4424a95da670a4fd6086b361a6e9f1.jpg" };
	private String[] hrefArray = { "https://www.baidu.com/",
			"http://www.alibabagroup.com/cn/global/home", "http://www.qq.com/" };
	
	//万能适配器以及图片异步加载
	private ListView listView1;
	private CommonAdapter commonAdapter;
	DisplayImageOptions options;
	protected ImageLoader imageLoader = ImageLoader.getInstance();
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_center, container,
				false);
		findView(rootView);
		initdata();
		initView();
		addListener();
		setActionBar();
		return rootView;
	}

	private void findView(View view){
		listView1=(ListView) view.findViewById(R.id.listView1);
	}
	private void initdata() {
		for (int i = 0; i < nameArray.length; i++) {
			PicBean picBean = new PicBean();
			picBean.setPicName(nameArray[i]);
			picBean.setPicUrl(picArray[i]);
			picBean.setPicHref(hrefArray[i]);
			picList.add(picBean);
		}
		
		options = new DisplayImageOptions.Builder()
		.showStubImage(R.drawable.ic_launcher) // 设置图片下载期间显示的图片
		.showImageForEmptyUri(R.drawable.ic_launcher) // 设置图片Uri为空或是错误的时候显示的图片
		.showImageOnFail(R.drawable.ic_launcher) // 设置图片加载或解码过程中发生错误显示的图片
		.cacheInMemory(true) // 设置下载的图片是否缓存在内存中
		.cacheOnDisc(true) // 设置下载的图片是否缓存在SD卡中
		.displayer(new RoundedBitmapDisplayer(20)) // 设置成圆角图片
		.build();
		imageLoader.init(ImageLoaderConfiguration
		.createDefault(getActivity()));
	}
	
	private void initView(){
		commonAdapter=new CommonAdapter<PicBean>(getActivity(), picList, R.layout.item_list_center) {

			@Override
			public void convert(ViewHolder helper, PicBean item) {
				helper.setText(R.id.textView1, item.getPicName());
				helper.setImageByUrl(R.id.imageView1, item.getPicUrl(), options,
						imageLoader);
				helper.setText(R.id.textView2, item.getPicHref());
			}
		};
		listView1.setAdapter(commonAdapter);
	}
	
	private void addListener(){
		listView1.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				String urlHref=picList.get(arg2).getPicHref();
				Intent cIntent=new Intent(getActivity(),WebActivity.class);
				Bundle bundle=new Bundle();
				bundle.putString("href", urlHref);
				cIntent.putExtra("bundle", bundle);
				startActivity(cIntent);
			}
		});
	}
	
	private void setActionBar(){
//		getActivity().getActionBar().set
	}
}
